package com.yongjie.springbootlogin.controller;

import com.yongjie.springbootlogin.entity.Notice;
import com.yongjie.springbootlogin.service.NoticeService;
import com.yongjie.springbootlogin.utils.PageResult;
import com.yongjie.springbootlogin.utils.ResultMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;

/**
 * 公告事务
 */
@Controller
public class noticeController {

    @Autowired
    private NoticeService noticeService;


    /**
     * 分页
     * @return
     */
    @RequestMapping(value = "/notice")
    public String allList(Map<String,Object>map,
                          HttpServletRequest request
                          ){
        //获取数据总条数
        Integer count=noticeService.selCount();
        map.put("count",count);
        String cupage=request.getParameter("currentPage");
        int currentPage;//默认当前页码为1
        int pageSize=15;//每页4条数据
        //计算总页数;
        int pages=(count+pageSize-1)/pageSize;
        //获取当前页码
        currentPage=(cupage==null)?1:Integer.parseInt(cupage);
        //如果当前页码小于1，则赋值为1，表示为首页
        if(currentPage<1) {
            currentPage=1;
        }
        //如果当前页数大于总页数，则赋值为总页数值，表示为尾页
        if(currentPage>pages) {
            currentPage=pages;
        }
        //将当前的页码保存在map中
        map.put("currentPage", currentPage);
        map.put("pages", pages);
        currentPage= (currentPage-1)*pageSize;
        if(currentPage<=0){
            currentPage=0;
        }
        //准备分页的数据
        List<Notice> NoticeInfo = noticeService.getUsersPages(currentPage, pageSize);
        map.put("NoticeInfo",NoticeInfo);
        return "notice/notice2";
    }
    //添加页面
    @RequestMapping("/addnotice")
    public String addmotice(){
        return "notice/addnotice";
    }

    //添加数据
    @PostMapping("/insNotice")
    public String insNotice( Map<String ,Object> map,Notice notice,
                             @RequestParam("title") String title,
                            @RequestParam(value = "content" ,required=false) String content,
                            @RequestParam("n_user")String n_user,
                            @RequestParam("classify")String classify,
                            @RequestParam("nt_state")String nt_state,
                             HttpServletRequest request
                            ){
            if(StringUtils.isEmpty(title)){
                map.put("msg","标题不能为空");
                return "notice/addnotice";
            }
            int count=noticeService.insNotice(title,content,n_user,classify,nt_state);
            if(count==0){
                map.put("msg","添加失败");
                return "notice/addnotice";
            }
            else{
                //获取新数据
             return   allList(map,request);
            }
    }

    /**
     * 删除数据
     * @param id
     * @return
     */
    @RequestMapping(value = "/delnotice")
    @ResponseBody
    public ResultMessage delnotice(Integer id){
        ResultMessage resultMessage=null;
        Integer count = noticeService.delnotice(id);
        if (count > 0) {
            resultMessage = new ResultMessage(200, "删除成功");
        } else {
            resultMessage = new ResultMessage(500, "删除失败");
        }
        return resultMessage;

    }
    /**
     * 查看单条公告
     */
    @RequestMapping("/noticeShow")
    public String noticeShow(int id,Model model){
        System.out.println(id+"------");
        Notice notice = noticeService.finnoticeById(id);
        model.addAttribute("notice",notice);
        return "notice/noticeShow";
    }
    /**
     * 分页
     * @return
     */
    @RequestMapping(value = "/DotroNotice")
    public String DotroNotice(Map<String,Object>map,
                          HttpServletRequest request
    ){
        //获取数据总条数
        Integer count=noticeService.selCount();
        map.put("count",count);
        String cupage=request.getParameter("currentPage");
        int currentPage;//默认当前页码为1
        int pageSize=15;//每页4条数据
        //计算总页数;
        int pages=(count+pageSize-1)/pageSize;
        //获取当前页码
        currentPage=(cupage==null)?1:Integer.parseInt(cupage);
        //如果当前页码小于1，则赋值为1，表示为首页
        if(currentPage<1) {
            currentPage=1;
        }
        //如果当前页数大于总页数，则赋值为总页数值，表示为尾页
        if(currentPage>pages) {
            currentPage=pages;
        }
        //将当前的页码保存在map中
        map.put("currentPage", currentPage);
        map.put("pages", pages);
        currentPage= (currentPage-1)*pageSize;
        if(currentPage<=0){
            currentPage=0;
        }
        //准备分页的数据
        List<Notice> NoticeInfo = noticeService.getUsersPages(currentPage, pageSize);
        map.put("NoticeInfo",NoticeInfo);
        return "notice/DoctroNotice";
    }
}

